﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Net.Class;
using Net.Properties;
using System.Text.RegularExpressions;
using Microsoft.Win32;
using System.Collections.Specialized;
namespace Net
{
    public partial class FormConfig : Form
    {
        public FormConfig()
        {
            InitializeComponent();
        }
        public static FormConfig childform = null;
        public static FormConfig signleton()
        {
            if (childform == null)
            {
                childform = new FormConfig();
            }
            return childform;
        }
        private void button1_confirm_Click(object sender, EventArgs e)
        {
            //if (checkBox1_disable.Checked)
            //{
            //    saveSettings();
            //    this.Close();
            //    return;
            //}
            string urlOut = this.textBox1_URL.Text.Trim();
            string urlIn = this.textBox1_URL.Text.Trim();
            //Regex r = new Regex("([w-]+.)+[w-]+.([^a-z])(/[w- ./?%&=]*)?|[a-zA-Z0-9-.][w-]+.([^a-z])(/[w- ./?%&=]*)?");
            //Match m = r.Match(tempURL);
            if (!Regex.IsMatch(urlOut, @"^http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$") || !Regex.IsMatch(urlIn, @"^http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$"))
            {
                MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "非法地址 ", "您填写的首选URL网址不合法，在调用浏览器访问时可能会导致程序错误！请重新输入\n合法网址形如：http://www.baidu.com", ToolTipIcon.Info);
                return;
            }
            if("".Equals(textBox1_time.Text)||Convert.ToInt32(textBox1_time.Text)<=0)
            {
                MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "输入非法 ", "时间的值只能为正整数，请重新输入", ToolTipIcon.Info);
                return;
            }
            if ("".Equals(textBox2_flow.Text) || Convert.ToInt32(textBox2_flow.Text) <= 0)
            {
                MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "输入非法 ", "流量的值只能为正整数，请重新输入", ToolTipIcon.Info);
                return;
            }
            if (Convert.ToInt32(textBox2_flow.Text) >1000)
            {
                MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "输入非法 ", "暂定流量的值不能超过1000，请重新输入", ToolTipIcon.Info);
                return;
            }
            if ("".Equals(textBox_money.Text))
            {
                MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "输入非法 ", "金额值不能为空，请重新输入", ToolTipIcon.Info);
                return;
            }
            if(Convert.ToDouble(textBox_money.Text)<=0)
            {
                MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "输入非法 ", "金额值必须大于0，请重新输入", ToolTipIcon.Info);
                return;
            }


            //tabpage3
            if (checkBox1_autoRun.Checked) //开机自动运行
            {
                //SetAutoRun(Application.StartupPath + "\\" + Application.ProductName + ".exe", true);
                SetAutoRun(Application.StartupPath + "\\" + "SwuEasyLogin" + ".exe", true);
            }
            else
            {
                //SetAutoRun(Application.StartupPath + "\\" + Application.ProductName + ".exe", false);
                SetAutoRun(Application.StartupPath + "\\" + "SwuEasyLogin" + ".exe", false);
            }
            if (checkBox_showStatus.Checked)//实时显示状态
            {
                MainForm.mainform.timer3.Enabled = true;
            }
            else
            {
                MainForm.mainform.timer3.Enabled = false;
            }

            //tabpage4
            string popmsg="";

            string url1 = this.textBox1_url1.Text.Trim();
            string url2 = this.textBox2_url2.Text.Trim();
            string url3 = this.textBox3_url3.Text.Trim();
            string url4 = this.textBox4_url4.Text.Trim();
            string url5 = this.textBox5_url5.Text.Trim();
            string title1 = textBox6_title1.Text.Trim();
            string title2 = textBox7_title2.Text.Trim();
            string title3 = textBox8_title3.Text.Trim();
            string title4 = textBox9_title4.Text.Trim();
            string title5 = textBox10_title5.Text.Trim();
            string[] urls = new string[5];
            urls[0] = url1;
            urls[1] = url2;
            urls[2] = url3;
            urls[3] = url4;
            urls[4] = url5;
            string[] titles = new string[5];
            titles[0] = title1;
            titles[1] = title2;
            titles[2] = title3;
            titles[3] = title4;
            titles[4] = title5;
            for (int i = 0; i < urls.Length; i++)
            {
                if (urls[i] != null && !urls[i].Equals(""))
                {
                    if (!Regex.IsMatch(urls[i], @"^http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$"))
                    {
                        popmsg = "您填写的常用网址不合法，在调用浏览器访问时可能会导致程序错误！请检查后重新输入\n合法网址形如：http://www.baidu.com";
                    }
                    else if (titles[i] == null || titles[i].Equals(""))
                    {
                        popmsg = "信息不完整。非空网址对应的标题不能为空，请填写完整！";
                    }

                }
            }
            if (!popmsg.Equals(""))
            {
                MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "常用网址输入非法 ", popmsg, ToolTipIcon.Info);
                return;
            }


           
            saveSettings();
            MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "保存设置成功", "部分功能需要重启程序后才能生效", ToolTipIcon.Info);
            this.Close();
        }

        private void saveSettings()
        {
            Settings.Default.isAutoIE = checkBox1_autoIE.Checked;
            Settings.Default.isIEDisabled = checkBox1_disable.Checked;
            Settings.Default.firstURL = textBox1_URL.Text.Trim();
            Settings.Default.firstUrlIn = textBox1_urlIn.Text.Trim();

            Settings.Default.isAutoQuitBytime = checkBox1_time.Checked;
            Settings.Default.isAutoQuitByflow = checkBox1_flow.Checked;
            Settings.Default.isAutoQuitByMoney = checkBox1.Checked;
            Settings.Default.autoQuitTime = textBox1_time.Text.Trim();
            Settings.Default.autoQuitFlow = textBox2_flow.Text.Trim();
            Settings.Default.autoQuitMoney = textBox_money.Text.Trim();

            Settings.Default.isAutorun = checkBox1_autoRun.Checked;
            Settings.Default.isAutoLoginStart = checkBox2_autoLogin.Checked;
            Settings.Default.isAutoLogoutQuit = checkBox3_autoLogout.Checked;
            Settings.Default.isLogEnabled = checkBox_isLogEnabled.Checked;
            Settings.Default.showStatus = checkBox_showStatus.Checked;

            //tabpage4
            string url1 = this.textBox1_url1.Text.Trim();
            string url2 = this.textBox2_url2.Text.Trim();
            string url3 = this.textBox3_url3.Text.Trim();
            string url4 = this.textBox4_url4.Text.Trim();
            string url5 = this.textBox5_url5.Text.Trim();
            string title1 = textBox6_title1.Text.Trim();
            string title2 = textBox7_title2.Text.Trim();
            string title3 = textBox8_title3.Text.Trim();
            string title4 = textBox9_title4.Text.Trim();
            string title5 = textBox10_title5.Text.Trim();
            //Settings.Default.url1 = url1;
            //Settings.Default.url2 = url2;
            //Settings.Default.url3 = url3;
            //Settings.Default.url4 = url4;
            //Settings.Default.url5 = url5;
            //Settings.Default.title1 = title1;
            //Settings.Default.title2 = title2;
            //Settings.Default.title3 = title3;
            //Settings.Default.title4 = title4;
            //Settings.Default.title5 = title5;

            //StringCollection scUrl = Settings.Default.urls;
            //StringCollection scTitle = Settings.Default.titles;
            string[] urls = new string[5];
            urls[0] = url1;
            urls[1] = url2;
            urls[2] = url3;
            urls[3] = url4;
            urls[4] = url5;
            string[] titles = new string[5];
            titles[0] = title1;
            titles[1] = title2;
            titles[2] = title3;
            titles[3] = title4;
            titles[4] = title5;
            if(urls.Length>0)
            {
                if (Settings.Default.urls == null)
                {
                    Settings.Default.urls = new StringCollection();
                }
                else
                {
                    Settings.Default.urls.Clear();
                }
                if (Settings.Default.titles == null)
                {
                    Settings.Default.titles = new StringCollection();
                }
                else
                {
                    Settings.Default.titles.Clear();
                }
               
                Settings.Default.urls.AddRange(urls);
                Settings.Default.titles.AddRange(titles);
            }
            //if (url1 != null && !url1.Equals("")) { scUrl.Add(url1); scTitle.Add(title1); }
            //if (url2 != null && !url2.Equals("")) { scUrl.Add(url2); scTitle.Add(title2); }
            //if (url3 != null && !url3.Equals("")) { scUrl.Add(url3); scTitle.Add(title3); }
            //if (url4 != null && !url4.Equals("")) { scUrl.Add(url4); scTitle.Add(title4); }
            //if (url5 != null && !url5.Equals("")) { scUrl.Add(url5); scTitle.Add(title5); }


            Settings.Default.Save();
        }

        private void FormConfig_Load(object sender, EventArgs e)
        {
            //tabpage1
            textBox1_URL.Text = Settings.Default.firstURL;
            textBox1_urlIn.Text = Settings.Default.firstUrlIn;
            checkBox1_autoIE.Checked = Settings.Default.isAutoIE;
            checkBox1_disable.Checked = Settings.Default.isIEDisabled;
            //tabpage2
            checkBox1_time.Checked = Settings.Default.isAutoQuitBytime;
            checkBox1_flow.Checked = Settings.Default.isAutoQuitByflow;
            checkBox1.Checked = Settings.Default.isAutoQuitByMoney;
            textBox1_time.Text = Settings.Default.autoQuitTime;
            textBox2_flow.Text = Settings.Default.autoQuitFlow;
            textBox_money.Text = Settings.Default.autoQuitMoney;
            //tabpage3
            checkBox1_autoRun.Checked = Settings.Default.isAutorun;
            checkBox2_autoLogin.Checked = Settings.Default.isAutoLoginStart;
            checkBox3_autoLogout.Checked = Settings.Default.isAutoLogoutQuit;
            checkBox_showStatus.Checked = Settings.Default.showStatus;
            checkBox_isLogEnabled.Checked = Settings.Default.isLogEnabled;
            if (checkBox1_disable.Checked)
            {
                textBox1_URL.Enabled = false;
                checkBox1_autoIE.Enabled = false;
            }
            if (!checkBox1_time.Checked) textBox1_time.Enabled = false;
            if (!checkBox1_flow.Checked) textBox2_flow.Enabled = false;
            if (!checkBox1.Checked) textBox_money.Enabled = false;

            //tabpage4
            StringCollection scUrl = Settings.Default.urls;
            if (Settings.Default.urls != null)
            {
                textBox1_url1.Text = Settings.Default.urls[0];
                textBox2_url2.Text = Settings.Default.urls[1];
                textBox3_url3.Text = Settings.Default.urls[2];
                textBox4_url4.Text = Settings.Default.urls[3];
                textBox5_url5.Text = Settings.Default.urls[4];

               // textBox1_url1.Enabled = false;
                //textBox2_url2.Enabled = false;
            }
            if (Settings.Default.titles != null)
            {

                textBox6_title1.Text = Settings.Default.titles[0];
                textBox7_title2.Text = Settings.Default.titles[1];
                textBox8_title3.Text = Settings.Default.titles[2];
                textBox9_title4.Text = Settings.Default.titles[3];
                textBox10_title5.Text = Settings.Default.titles[4];

                //textBox6_title1.Enabled = false;
                //textBox7_title2.Enabled = false;
            }
            //textBox1_url1.Text = Settings.Default.url1;
            //textBox2_url2.Text = Settings.Default.url2;
            //textBox3_url3.Text = Settings.Default.url3;
            //textBox4_url4.Text = Settings.Default.url4;
            //textBox5_url5.Text = Settings.Default.url5;

            //textBox6_title1.Text = Settings.Default.title1;
            //textBox7_title2.Text = Settings.Default.title2;
            //textBox8_title3.Text = Settings.Default.title3;
            //textBox9_title4.Text = Settings.Default.title4;
            //textBox10_title5.Text = Settings.Default.title5;
        }

        private void button2_back_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void checkBox1_disable_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1_disable.Checked)
            {
                textBox1_URL.Enabled = false;
                checkBox1_autoIE.Enabled = false;
            }
            else
            {
                textBox1_URL.Enabled = true;
                checkBox1_autoIE.Enabled = true;
            }
        }

        private void checkBox1_time_CheckedChanged(object sender, EventArgs e)
        {
            //if (checkBox1_time.Checked)
            //{
            //    textBox1_time.Enabled = true;
            //}
            //else
            //{
            //    textBox1_time.Enabled = false;
            //}
            textBox1_time.Enabled = checkBox1_time.Checked ? true : false;
        }

        private void checkBox1_flow_CheckedChanged(object sender, EventArgs e)
        {
            textBox2_flow.Enabled = checkBox1_flow.Checked ? true : false;
        }

        private void onKeyUp(object sender, KeyEventArgs e)
        {
            Regex regex = new Regex(@"[^0-9]");
            textBox1_time.Text = regex.Replace(textBox1_time.Text.Trim(), "");  
        }

        private void onLlowKeyUp(object sender, KeyEventArgs e)
        {
            Regex regex = new Regex(@"[^0-9]");
            textBox2_flow.Text = regex.Replace(textBox2_flow.Text.Trim(), "");  
        }

        private void formClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                this.Dispose(true);
                childform = null;
            }
            catch (Exception ex)
            { MainForm.mainform.notifyIcon1.ShowBalloonTip(2000, "出现异常 ", ex.ToString(), ToolTipIcon.Info); }
        }

        //private void textBox1_time_TextChanged(object sender, EventArgs e)
        //{
            
        //}
        public void SetAutoRun(string fileName, bool isAutoRun)
        {
            RegistryKey reg = null;
            try
            {
                //Log.WriteLine("SetAutoRun", fileName);
                if (!System.IO.File.Exists(fileName))
                    throw new Exception("该文件不存在!");
                String name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
                reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if (reg == null)
                    reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                if (isAutoRun)
                    reg.SetValue(name, fileName);
                else
                    reg.SetValue(name, false);
                //MessageBox.Show("设定成功！", "提示");
            }
            catch(Exception ex)
            {
                //throw new Exception(ex.ToString()); 
                Log.WriteLine("SetAutoRun", ex.ToString());
            }
            finally
            {
                if (reg != null)
                    reg.Close();
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

            textBox_money.Enabled = checkBox1.Checked ? true : false;
        }

      

        private void onMoneyKeyUp(object sender, KeyEventArgs e)
        {
            //Regex regex = new Regex(@"[^0-9]");
            //textBox_money.Text = regex.Replace(textBox_money.Text.Trim(), "");  
        }

        private void onMoneyKeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < '0' && e.KeyChar != '.' || e.KeyChar > '9' && e.KeyChar != '.' || ((TextBox)(sender)).Text.IndexOf('.') >= 0 && e.KeyChar == '.') && e.KeyChar != (char)13 && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }

        }

        
        
    }
}
